/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * KmeansOptionsPanel.java
 *
 * Created on 25-févr.-2009, 22:53:03
 */

package View.ClusterizationPanel;


import View.ClusterizationPanel.ColorEditor;
import View.ClusterizationPanel.ClusterizationWindow;
import Model.AttributeType;
import java.awt.Color;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author vf
 */
public class KmeansOptionsPanel extends OptionsPanel {

    /** Creates new form KmeansOptionsPanel */
    public KmeansOptionsPanel(){super();}

    public void setParent(ClusterizationWindow mainWin) {
        
        this.mainWin = mainWin;

        initComponents();
        tableClusters.setModel(new ClustersTableModel());
        textFieldName.setText("Clusterization"+(getNumberOfClusterizations()+1));
        
        tableClusters.setDefaultRenderer(Color.class, new ColorRenderer(true));
        tableClusters.getColumn("Color").setCellEditor(new ColorEditor());
        //textFieldName.setVisible(false);
        //labelName.setVisible(false);
        System.out.println(getWidth());
        setInitialValues();
        this.setVisible(true);
        this.setOpaque(true);
        this.setSize(80,500);
    }

    private void setInitialValues() {
        for (int i=0;i<mainWin.getNumberOfDistances();i++)
            comboBoxDistance.addItem(mainWin.getDistanceType(i));
        checkBoxNormalize.setSelected(true);
        spinnerClusters.setValue(3);//tableClusters se met à jour à travers spinnerClustersStateChanged()
        for (int i=0;i<mainWin.getAttributeNbr();i++)
        {
            ((DefaultTableModel)tableWeights.getModel()).addRow(new Object[2]);
            tableWeights.setValueAt(mainWin.getAttributeName(i),i,0);
            if(mainWin.getAttributeType(i)==AttributeType.Numerical)tableWeights.setValueAt(1.0f,i,1);
            else tableWeights.setValueAt(0.0f,i,1);
        }
        tableClusters.getColumn("No").setPreferredWidth(20);
        tableWeights.getColumn("Weight").setPreferredWidth(20);
        textAreaInfo.setEditable(false);
        textAreaInfo.setLineWrap(true);
        textAreaInfo.setWrapStyleWord(true);
        printInfo("Algorythm used: Kmeans",Color.BLACK);
    }

    private void addCluster(){
        //Nouveau cluster toujours ajouté à la fin du tableau
        int rowNo = tableClusters.getRowCount();
        int columns = tableClusters.getColumnCount();
        ((DefaultTableModel)tableClusters.getModel()).addRow(new Object[columns]);
        tableClusters.setValueAt(rowNo+1, rowNo, 0);
        tableClusters.setValueAt("Cluster"+(rowNo+1), rowNo, 1);
        tableClusters.setValueAt(nextColor(rowNo), rowNo, 2);
    }

    private void updateInfo(String clustName){
        String message=new String(
                "Algorythm used: Kmeans \n"+
                "Current: "+clustName+"\n"+
                (Integer)spinnerClusters.getValue()+" Clusters\n"+
                "Attribute's weights: \n"
                );
        for (int i=0;i<tableWeights.getRowCount();i++)
            message = message.concat(tableWeights.getValueAt(i,0)+": "+
                    ((Float)tableWeights.getValueAt(i,1)).floatValue()+"\n");
        printInfo(message,Color.BLACK);
    }

    private void printInfo (String message, Color color){
        textAreaInfo.setText(message);
        textAreaInfo.setForeground(color);
        textAreaInfo.setCaretPosition(0);//Curseur au début du texte. "Remonte la scroll bar"
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        comboBoxDistance = new javax.swing.JComboBox();
        checkBoxNormalize = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        buttonClusterize = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        spinnerClusters = new javax.swing.JSpinner();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableClusters = new javax.swing.JTable();
        jScrollPane3 = new javax.swing.JScrollPane();
        tableWeights = new javax.swing.JTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        textAreaInfo = new javax.swing.JTextArea();
        textFieldName = new javax.swing.JTextField();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        setPreferredSize(new java.awt.Dimension(200, 550));

        checkBoxNormalize.setText("Normalize");
        checkBoxNormalize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkBoxNormalizeActionPerformed(evt);
            }
        });

        jLabel1.setText("Distance:");

        buttonClusterize.setText("Clusterize");
        buttonClusterize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonClusterizeActionPerformed(evt);
            }
        });

        jLabel2.setText("Attribute's relative weights:");

        jLabel3.setText("Clusters:");

        spinnerClusters.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                spinnerClustersStateChanged(evt);
            }
        });

        tableClusters.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tableClusters);

        tableWeights.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Attribute", "Weight"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tableWeights);

        jScrollPane5.setHorizontalScrollBar(null);

        textAreaInfo.setColumns(20);
        textAreaInfo.setRows(5);
        jScrollPane5.setViewportView(textAreaInfo);

        textFieldName.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(12, 12, 12)
                        .addComponent(spinnerClusters, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(buttonClusterize, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(jLabel2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addComponent(textFieldName, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(checkBoxNormalize)
                            .addComponent(comboBoxDistance, 0, 131, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textFieldName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spinnerClusters, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(comboBoxDistance, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(checkBoxNormalize)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonClusterize)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void checkBoxNormalizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBoxNormalizeActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_checkBoxNormalizeActionPerformed

    private void spinnerClustersStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerClustersStateChanged
        int oldNumberOfRows = tableClusters.getRowCount();
        int newNumberOfRows = ((Integer)spinnerClusters.getValue()).intValue();
        if (newNumberOfRows > oldNumberOfRows){
            for (int i=oldNumberOfRows;i<newNumberOfRows;i++)
                addCluster();
        }
        else if (newNumberOfRows < oldNumberOfRows){
            for (int i=newNumberOfRows;i<oldNumberOfRows;i++)
                ((DefaultTableModel)tableClusters.getModel()).removeRow(i);
        }
    }//GEN-LAST:event_spinnerClustersStateChanged

    private void buttonClusterizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonClusterizeActionPerformed
            clusterize(textFieldName.getText());
    }//GEN-LAST:event_buttonClusterizeActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonClusterize;
    private javax.swing.JCheckBox checkBoxNormalize;
    private javax.swing.JComboBox comboBoxDistance;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTable jTable1;
    private javax.swing.JSpinner spinnerClusters;
    private javax.swing.JTable tableClusters;
    private javax.swing.JTable tableWeights;
    private javax.swing.JTextArea textAreaInfo;
    private javax.swing.JTextField textFieldName;
    // End of variables declaration//GEN-END:variables

    @Override
    public void clusterize(String name){
        mainWin.createKmeans(name);
        updateInfo(name);
        if (firstClusterization){
            firstClusterization = false;
            textFieldName.setText(name);
            textFieldName.setVisible(true);
            //labelName.setVisible(true);
            buttonClusterize.setText("Reclusterize");
        }
        mainWin.setTitle("Clusterization View: "+name);
    }

    @Override
    public int getNumberOfClusters() {
        return (Integer)spinnerClusters.getValue();
    }

    @Override
    public boolean getNormalisationValue() {
        return checkBoxNormalize.isSelected();
    }

    @Override
    public String getDistanceValue() {
        return (String)comboBoxDistance.getSelectedItem();
    }

    @Override
    public String getClusterName(int i) {
        return (String)tableClusters.getValueAt(i,1);
    }

    @Override
    public Color getClusterColor(int i) {
        return (Color)tableClusters.getValueAt(i,2);
    }

    @Override
    public float getAttributeWeight(int i) {
        return (Float)tableWeights.getValueAt(i,1);
    }

    @Override
    public void closeWin() {
    }


    

}
